package com.ruoyi.web.controller.kujiale;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JYXResult;
import com.ruoyi.system.domain.DesignScheme;
import com.ruoyi.system.domain.DesignSchemeImage;
import com.ruoyi.system.enums.SpaceTypeEnum;
import com.ruoyi.system.service.DesignSchemeImageService;
import com.ruoyi.system.service.DesignSchemeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 设计方案Controller
 */
@RestController
@RequestMapping("/system/designScheme")
@Api(tags = "设计方案管理")
public class DesignSchemeController extends BaseController {

    @Autowired
    private DesignSchemeService designSchemeService;

    @Autowired
    private DesignSchemeImageService imageService;

    /**
     * 根据在线设计id查询方案详情
     */
    @GetMapping("/getDesign/{onlineId}")
    @ApiOperation("根据在线设计id查询方案详情")
    public JYXResult<DesignScheme> getDesignSchemeByOnlineId(@PathVariable String onlineId) {
        DesignScheme scheme = designSchemeService.getDesignSchemeByOnlineId(onlineId);
        if (scheme != null) {
            List<DesignSchemeImage> images = imageService.selectBySchemeId(scheme.getId());
            // 设置空间名称用于前端显示
            for (DesignSchemeImage image : images) {
                SpaceTypeEnum type = SpaceTypeEnum.getByValue(image.getSpaceType());
                if (type != null) {
                    image.setSpaceName(type.getLabel());
                }
            }
            scheme.setImages(images);
        }

        return JYXResult.success(scheme);
    }
    /**
     * 根据基础设计ID查询所有方案
     */
    @GetMapping("/listByBaseId/{designBaseId}")
    @ApiOperation("根据基础设计ID查询所有方案")
    public AjaxResult listByBaseId(@PathVariable Long designBaseId) {
        return AjaxResult.success(designSchemeService.selectByDesignBaseId(designBaseId));
    }

    /**
     * 获取设计方案详情
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取设计方案详情")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        DesignScheme scheme = designSchemeService.getById(id);
        if (scheme != null) {
            List<DesignSchemeImage> images = imageService.selectBySchemeId(id);
            // 设置空间名称用于前端显示
            for (DesignSchemeImage image : images) {
                SpaceTypeEnum type = SpaceTypeEnum.getByValue(image.getSpaceType());
                if (type != null) {
                    image.setSpaceName(type.getLabel());
                }
            }
            scheme.setImages(images);
        }
        return AjaxResult.success(scheme);
    }

    /**
     * 新增设计方案
     */
    @PostMapping
    @ApiOperation("新增设计方案")
    public AjaxResult add(@RequestBody DesignScheme designScheme) {
        return toAjax(designSchemeService.saveSchemeWithImages(designScheme) ? 1 : 0);
    }

    /**
     * 修改设计方案
     */
    @PutMapping
    @ApiOperation("修改设计方案")
    public AjaxResult edit(@RequestBody DesignScheme designScheme) {
        return toAjax(designSchemeService.updateSchemeWithImages(designScheme) ? 1 : 0);
    }

    /**
     * 删除设计方案
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除设计方案")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(designSchemeService.removeSchemeWithImages(id) ? 1 : 0);
    }

    /**
     * 获取所有空间类型
     */
    @GetMapping("/getSpaceTypes")
    @ApiOperation("获取所有空间类型")
    public AjaxResult getSpaceTypes() {
        return AjaxResult.success(SpaceTypeEnum.getAllTypes());
    }

    /**
     * 检查文件名是否存在
     */
    @GetMapping("/checkFileName")
    @ApiOperation("检查文件名是否存在")
    public AjaxResult checkFileName(@RequestParam Long schemeId,
                                    @RequestParam String spaceType,
                                    @RequestParam String fileName) {
        boolean exists = imageService.checkFileNameExists(schemeId, spaceType, fileName);
        if (exists) {
            return AjaxResult.error("该文件名已存在，请修改后上传");
        }
        return AjaxResult.success();
    }

    /**
     * 更新图片排序
     */
    @PostMapping("/updateImageSort")
    @ApiOperation("更新图片排序")
    public AjaxResult updateImageSort(@RequestBody List<DesignSchemeImage> images) {
        return toAjax(imageService.updateImageSort(images) ? 1 : 0);
    }

    /**
     * 上传图片
     */
    @PostMapping("/uploadImage")
    @ApiOperation("上传图片")
    public AjaxResult uploadImage(@RequestParam Long schemeId,
                                  @RequestParam String spaceType,
                                  @RequestParam String fileName,
                                  @RequestParam MultipartFile file) {
        // 实际项目中需要实现文件上传逻辑
        // 1. 验证文件类型和大小
        // 2. 上传文件到服务器或云存储
        // 3. 保存文件信息到数据库
        return AjaxResult.success();
    }

    /**
     * 删除方案图片
     */
    @DeleteMapping("/deleteImage/{id}")
    @ApiOperation("删除方案图片")
    public AjaxResult deleteImage(@PathVariable Long id) {
        return toAjax(imageService.removeById(id) ? 1 : 0);
    }
}
